package ua.amsoft.brelok.dao;

import ua.amsoft.brelok.db.DatabaseOpenHelper;
import ua.amsoft.brelok.dto.UserInputDocsReports;
import ua.amsoft.brelok.manager.DatabaseManager;
import ua.amsoft.brelok.util.BrelokUtils;
import ua.amsoft.brelok.util.DateTimeUtils;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;

/**
 * @author Tair Mustafaiev
 */
public class UserInputDocsReportsDAO implements IUserInputDocsReportsDAO {
    
    private static final String SELECT_ALL = "select * from " + DatabaseOpenHelper.UIDR_TABLE_NAME;
    
    private static final String TAG = UserInputDocsReportsDAO.class.getCanonicalName();
    
    private final DatabaseManager databaseManager;
    
    /**
     * Constructor
     * 
     * @param context {@link Context}
     */
    public UserInputDocsReportsDAO(final Context context) {
        this.databaseManager = new DatabaseManager(context);
    }
    
    @Override
    public void insert(final ContentValues values) {
        try {
            this.databaseManager.open();
            this.databaseManager.insert(DatabaseOpenHelper.UIDR_TABLE_NAME, null, values);
        } finally {
            this.databaseManager.close();
        }
    }
    
    @Override
    public UserInputDocsReports get() {
        try {
            this.databaseManager.open();
            final String query = UserInputDocsReportsDAO.SELECT_ALL;
            final Cursor result = this.databaseManager.select(query, null);
            final UserInputDocsReports reports = BrelokUtils.parseObjectFromCursor(
                    UserInputDocsReports.class, result, DatabaseOpenHelper.UIDR_FILE_COLUMN);
            final String date = result.getString(result
                    .getColumnIndex(DatabaseOpenHelper.UIDR_DATE_UPDATED_COLUMN));
            reports.setUpdatedDate(DateTimeUtils.parseOutputDate(date));
            return reports;
        } catch (final Exception e) {
            Log.e(UserInputDocsReportsDAO.TAG, e.getLocalizedMessage());
        } finally {
            this.databaseManager.close();
        }
        return null;
    }
    
    @Override
    public void update(final ContentValues values, final String whereClause,
            final String[] whereArgs) {
        try {
            this.databaseManager.open();
            this.databaseManager.executeUpdate(DatabaseOpenHelper.UIDR_TABLE_NAME, values,
                    whereClause, whereArgs);
        } finally {
            this.databaseManager.close();
        }
    }
    
    @Override
    public void deleteAll() {
        try {
            this.databaseManager.open();
            this.databaseManager.delete(DatabaseOpenHelper.UIDR_TABLE_NAME, null, null);
        } finally {
            this.databaseManager.close();
        }
    }
    
    @Override
    public void forceUpdateTable(final ContentValues values) {
        try {
            this.databaseManager.open();
            this.databaseManager.delete(DatabaseOpenHelper.UIDR_TABLE_NAME, null, null);
            this.databaseManager.insert(DatabaseOpenHelper.UIDR_TABLE_NAME, null, values);
        } finally {
            this.databaseManager.close();
        }
    }
    
}
